package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bwie.poi.StringUtils;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;
import com.bwie.vo.CodeVo;
import com.bwie.vo.DateVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.mapper.UserMapper;
import com.bwie.service.UserService;
/**
 * @Author: zhang
 * @Date:  2025/7/4 09:07
 * @Description: 
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Override
    public R sendPhoneCode(CodeVo codeVo) {
        if(StringUtils.isEmpty(codeVo.getPhone())){
            return R.ERROR(500,"手机号不能为空");
        }
        if(StringUtils.isEmpty(codeVo.getPictureCode())){
            return R.ERROR(500,"图片验证码不能为空");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,codeVo.getPhone());
        User user = userMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(user)){
            return R.ERROR(500,"手机号不正确，请稍后重新发送");
        }
        String picture = redisTemplate.opsForValue().get("图片验证码"+codeVo.getPictureCode());
        if(StringUtils.isEmpty(picture)){
            return R.ERROR(500,"图片验证码失效了");
        }
        if(ObjectUtils.isNotEmpty(user)){

            String phone1 = redisTemplate.opsForValue().get("一分钟" + user.getPhone());
            if(StringUtils.isEmpty(phone1)){
                kafkaTemplate.send("hao-fei",user.getPhone());

            }else {
                return R.ERROR(500,"请在一分钟后尝试");

            }

        }
            return R.OK();
    }

    @Override
    public R login(CodeVo codeVo) {
        if(StringUtils.isEmpty(codeVo.getPhone())){
            return R.ERROR(500,"手机号不能为空");
        }
        if(StringUtils.isEmpty(codeVo.getPictureCode())){
            return R.ERROR(500,"图片验证码不能为空");
        }
        if(StringUtils.isEmpty(codeVo.getPhoneCode())){
            return R.ERROR(500,"图片验证码不能为空");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> eq = wrapper.eq(User::getPhone, codeVo.getPhone());
        User user = userMapper.selectOne(eq);
        if(!user.getPhone().equals(codeVo.getPhone())){
            return R.ERROR(500,"手机号错误");
        }
        String s = redisTemplate.opsForValue().get("图片验证码" + codeVo.getPictureCode());
        if(StringUtils.isEmpty(s)){
            return R.ERROR(500,"图片验证码失效");
        }
        String s1 = redisTemplate.opsForValue().get(codeVo.getPhone());
        if(StringUtils.isEmpty(s1)){
            return R.ERROR(500,"手机号验证码失效");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("id", user.getId()).compact();
        redisTemplate.opsForValue().set("token",token,30,TimeUnit.MINUTES);
        user.setToken(token);
        return R.OK(user);
    }

    @Override
    public List<DateVo> selectAllOrder() {
        return userMapper.selectAllOrder();
    }

    @Override
    public ArrayList<String> selectWeekPeople() {
        return userMapper.selectWeekPeople();
    }

    @Override
    public ArrayList<Integer> selectWeekPeopleY1() {
        return userMapper.selectWeekPeopleY1();
    }

    @Override
    public ArrayList<Integer> selectWeekPeopleY2() {
        return userMapper.selectWeekPeopleY2();
    }

    @KafkaListener(topics = "hao-fei")
    public void seletKafkaTemplate(String phone, Acknowledgment acknowledgment){
        String s = RandomUtil.randomNumbers(6);
        Boolean aBoolean = MobileMessageUtil.sendMobileMessage(phone, s);
        if(aBoolean){
            System.out.println("发送成功");
            System.out.println("手机号验证码:"+s);
        }


        redisTemplate.opsForValue().set(phone,s,5, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("一分钟"+phone,s,1, TimeUnit.MINUTES);
        acknowledgment.acknowledge();
    }
}
